import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzMessageService, UploadChangeParam, UploadFile } from 'ng-zorro-antd';
import { FetchService } from '../../../core/net/fetch.service';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'app-pm-project-files-detail',
  templateUrl: './pm-project-files-detail.component.html',
  styleUrls: ['./pm-project-files-detail.component.scss']
})
export class PmProjectFilesDetailComponent implements OnInit {
    projectId;
    form: FormGroup;
    yearOption = [];
    typeOption = [
        {value: 'all', label: '全部'},
        {value: '1', label: '涉外工程'},
        {value: '2', label: '岁修工程'},
        {value: '3', label: '基建工程'},
    ];
    tableData;
    fileUploadForm: FormGroup;
    nzBodyStyle: any = {
        padding: '16px 20px 0'
    };
    fileList: UploadFile[] = [];
    uploadUrl = `${(window as any).environment.apiBaseUrl}comm/file/upload`;
    fileModalTitle = '文件上传';
    fileModalShow = false;
    uploadFileId = '';

    constructor(private fb: FormBuilder,
                private message: NzMessageService,
                private fetchService: FetchService,
                private routerInfo: ActivatedRoute,
                private router: Router
    ) {
        const date = new Date();
        const year = date.getFullYear();
        for (let i = year - 10; i <= year; i++) {
            this.yearOption.unshift(i);
        }
        this.form = this.fb.group({
            id: [null],
            name: [null, Validators.required],
            year: [null, Validators.required],
            manager: [null, Validators.required],
            telephone: [null, Validators.required],
            type: [null, Validators.required],
            districtId: [null],
            address: [null, Validators.required],
            remark: [null, Validators.required]
        });

        this.fileUploadForm = this.fb.group({
            projectId: [null],
            fileName: [null],
            fileTypeName: [null],
            num: [null],
            version: [null]
        });
        this.projectId = this.routerInfo.snapshot.queryParams['id'];
    }

    ngOnInit(): void {
        if (this.projectId) {
            this.checkDetail();
        }
    }

    checkDetail() {
        this.fetchService.get<any>(`archives/wrsProjectInfo/${this.projectId}`).subscribe(res => {
            /* this.tableData = [
               {id: '1', name: '前期工作文件及批复文件'},
               {id: '2', name: '主管部门批文'},
               {id: '3', name: '招标投标文件'},
               {id: '4', name: '合同文件'},
               {id: '5', name: '工程项目划分资料'},
               {id: '6', name: '单元工程质量评定资料'},
               {id: '7', name: '分部工程质量评定资料'},
               {id: '8', name: '单位工程质量评定资料'},
               {id: '9', name: '工程外观质量评定资料'},
               {id: '10', name: '工程质量管理有关文件'},
               {id: '11', name: '工程安全管理有关文件'},
               {id: '12', name: '工程施工质量检验文件'},
               {id: '13', name: '工程监理资料'},
               {id: '14', name: '施工图设计文件'},
               {id: '15', name: '工程设计变更资料'},
               {id: '16', name: '竣工图纸'},
               {id: '17', name: '征地移民有关文件'},
               {id: '18', name: '重要会议记录'},
               {id: '19', name: '质量缺陷备案表'},
               {id: '20', name: '安全、质量事故资料'},
               {id: '21', name: '阶段验收鉴定书'},
               {id: '22', name: '竣工决算及审计资料'},
               {id: '23', name: '工程建设中使用的技术标准'},
               {id: '24', name: '工程建设标准强制性条文'},
               {id: '25', name: '专项验收有关文件'},
               {id: '26', name: '安全、技术鉴定报告'},
               {id: '27', name: '其他档案资料'}
             ];*/
            this.tableData = res.projectFiles;
            this.form.patchValue(res);
        });
    }

    handleChange({file, fileList}: UploadChangeParam): void {
        const status = file.status;
        this.uploadFileId = '';
        if (status !== 'uploading') {
            // console.log(file, fileList);
        }
        if (status === 'done') {
            this.uploadFileId = file.response.data.id;
        } else if (status === 'error') {

        }
    }

    openFileUploadModal(item) {
        this.fileUploadForm.patchValue(item);
        this.fileList = [];
        this.fileModalShow = true;
    }

    onConfirm() {
        this.form.markAsTouched();
        if (!this.form.valid) {
            this.message.warning('信息填写有误, 请检查');
            return;
        }
        const data = this.form.value.type === 'all' ? {
            ...this.form.value,
            type: ''
        } : this.form.value;
        if (data.id) {
            this.fetchService.post('archives/wrsProjectInfo/edit', data).subscribe(res => {
                this.message.success('操作成功！');
                this.backToList();
            });
        } else {
            this.fetchService.post('archives/wrsProjectInfo/add', data).subscribe(res => {
                this.message.success('操作成功！');
                this.backToList();
            });
        }
    }

    onFileConfirm(): void {
        if (!this.uploadFileId) {
            this.message.error('请上传附件');
            return;
        }
        const param = {
            projectId: this.fileUploadForm.value.projectId,
            fileId: this.uploadFileId,
            num: this.fileUploadForm.value.num,
            version: this.fileUploadForm.value.version
        };
        this.fetchService.post('archives/wrsProjectFile/add', param).subscribe(res => {
            this.message.success('操作成功！');
            this.fileModalShow = false;
            this.checkDetail();
        });
    }

    onFileCancel(): void {
        this.fileModalShow = false;
    }


    backToList() {
        window.history.back();
    }

    versionManage() {
        this.router.navigate(['/pm/history-version']);
    }
}
